<?php

namespace MyApp\Controller;

use RedBean_Facade as R;

class HomeController extends \SlimController\SlimController
{
    public function indexAction()
    {
        //$userinfo = $this->app->request->post('userinfo');
        $this->render('home/game');
    }
    
    public function testAction()
    {
        die('end');
    }
    
    public function gameAction()
    {
        $_SESSION['gamed_at'] = time();
        $this->render('home/game');
    }
    
    public function submitAction()
    {
        if (!isset($_SESSION['gamed_at']) || time() - $_SESSION['gamed_at'] < 5) {
            exit;
        }
        unset($_SESSION['gamed_at']);
        
        $app = $this->app;
        $score = intval($app->request->post('score'));
        $_SESSION['score'] = $score;
        if ($score < 3000000) { // 单局不可能超过300W
            $app->user->coins += $score;
            if ($app->user->coins >= 10000000000) { // 用户金币总数不允许超过100亿
                $app->user->coins = 10000000000;
            }
            R::store($app->user);
        }
        
        $app->contentType('application/json');
        $app->response->write(json_encode(['ok'=>true]));
        return $app->response;
    }
    public function searchAction()
    {
        $app = $this->app;
        $mobile = $app->request->get('mobile');
        $rewards  = R::findAll('winner', ' mobile = ? ', [ $mobile ]);
        $data = [0,0,0];
        $flag = false;
        foreach ($rewards as $r) {
            $reward = $r['reward'] - 1;
            $data[$reward] ++;
            $flag = true;
        }
        //print_r($data);
        $this->render('home/search', [
            'data' => $data,
            'flag' => $flag
        ]);
    }
   
    public function ruleAction()
    {
        $this->render('home/rule');
    }
    
    public function friendAction($uuid)
    {
        $this->render('home/index');
    }
    
    // 抽奖
    public function drawAction()
    {
        $app = $this->app;
        $user = $app->user;
        $friendship  = R::findAll('friendship', ' uid = ? ', [ $user->id ]);
        $friendIds = array();
        foreach ($friendship as $f) {
            $friendIds[] = $f['fid'];
        }
        $friendIds = array_unique($friendIds);
        if( !empty($friendIds)){
            $friends = R::findAll('user', 'id IN (' . R::genSlots($friendIds) . ')', $friendIds);
        } else {
            $friends =[];
        }
            
        $this->render('home/draw', [
            'friends' => $friends
        ]);
    }
    
    public function receiptAction()
    {
        $app = $this->app;
//        $uuid = $app->getCookie('uuid');
//        if (!$uuid) {
//            $this->redirect($app->urlFor('index'));
//        }
        
        $req = $app->request;
        $reward = $req->get('reward');
        if ($req->post('submit')) {
            $user = $app->user;
            $mobile = trim($req->post('mobile'));
            $user->name   = trim($req->post('name'));
            $user->mobile = $mobile;
            $user->updatedAt = R::isoDateTime();
            
            R::store($user);
            
            header('Content-Type: application/json');
            echo json_encode(['ok'=>true]);
            exit;
        }
        
        $this->render('home/receipt', [ 'reward' => $reward ]);
    }
    
    public function resultAction()
    {
        $score = $_SESSION['score'];
        $this->render('home/result',['score' => $score]);
    }
    
    public function sharecntAction()
    {
        $app = $this->app;
        $stats = R::findOne('stats', ' sid = 1 ');
        if (!$stats) {
            $stats = R::dispense('stats');
            $stats->sid = 1;
            $stats->nbFriends   = 0;
            $stats->nbTimelines = 0;
        }
        if ($app->request->get('f') == 1) {
            $stats->nbFriends += 1;
        } else {
            $stats->nbTimelines += 1;
        }
        R::store($stats);
        
        header('Content-Type: application/json');
        echo json_encode(['ok'=>1]);
        exit;
    }
}